package com.jbyu.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.jbyu.db.DBHelp;
import com.jbyu.entity.Column;
import com.jbyu.entity.Schema;
import com.jbyu.entity.Table;
public class ORMDAO {
	
	public List<Schema> getAllSchema(String sqlKey){
		Connection con = null;
		PreparedStatement statement = null;
		ResultSet set=null;
		List<Schema> result=new ArrayList<Schema>();
		try {
			con = DBHelp.getCon();
			statement = con.prepareStatement(sqlKey);
			set=statement.executeQuery();
			while (set.next()) {
				Schema s = new Schema();
				s.setSchemaName(set.getString("schemaName"));
				result.add(s);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBHelp.closeAll(con, statement, set);
		}
	  return result;
	}
	
	
	public List<Table> getAllTable(String sqlKey,String schemaName){
		Connection con = null;
		PreparedStatement statement = null;
		ResultSet set=null;
		List<Table> result=new ArrayList<Table>();
		try {
			con = DBHelp.getCon();
			statement = con.prepareStatement(sqlKey);
			statement.setObject(1, schemaName);
			set=statement.executeQuery();
			while (set.next()) {
				Table s = new Table();
				s.setTableName(set.getString("tableName"));
				s.setTableCommnets(set.getString("tableCommnets"));
				result.add(s);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBHelp.closeAll(con, statement, set);
		}
	  return result;	
	}
	
	public List<Column> getAllColumn(String sqlKey,String tableName){
		Connection con = null;
		PreparedStatement statement = null;
		ResultSet set=null;
		List<Column> result=new ArrayList<Column>();
		try {
			con = DBHelp.getCon();
			statement = con.prepareStatement(sqlKey);
			statement.setObject(1, tableName);
			set=statement.executeQuery();
			while (set.next()) {
				Column s = new Column();
				s.setClumnComments(set.getString("clumnComments"));
				s.setClumnIsNull(set.getString("clumnIsNull"));
				s.setClumnLength(String.valueOf(set.getLong("clumnLength")));
				s.setClumnName(set.getString("clumnName"));
				s.setClumnPK(set.getString("clumnPK"));
				s.setClumnType(set.getString("clumnType"));
				result.add(s);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBHelp.closeAll(con, statement, set);
		}
	  return result;		
	}
	
	
	

}
